package com.bustle.web.mbean.request;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import com.bustle.order.data.Account;
import com.bustle.order.data.Lineitem;
import com.bustle.order.data.Order;
import com.bustle.order.data.Order.OrderStatus;
import com.bustle.order.data.Payment;
import com.bustle.order.data.dao.AccountDao;
import com.bustle.order.data.dao.LineitemDao;
import com.bustle.order.data.dao.OrderDao;
import com.bustle.order.data.dao.PaymentDao;
import com.bustle.product.data.Product;
import com.bustle.product.data.Product.ProductType;
import com.bustle.product.data.Provider;
import com.bustle.product.data.dao.ProductDao;
import com.bustle.product.data.dao.ProviderDao;
import com.bustle.user.data.Address;
import com.bustle.user.data.Authentication;
import com.bustle.user.data.User;
import com.bustle.user.data.dao.AddressDao;
import com.bustle.user.data.dao.AuthenticationDao;
import com.bustle.user.data.dao.UserDao;
import com.bustle.web.mbean.session.CartMBean;

@ManagedBean
@RequestScoped
public class InjectDBMBean
{
  public String inject()
  {

    //  Address
    Address address = new Address();
    address.setName_street("rue de la boetie");
    address.setCity("ville provider");
    address.setPostcode("75011");
    addressDao.create(address);

    //Provider
    Provider provider = new Provider();
    provider.setAddress(address);
    provider.setDescription("Musée français bla bla bla description");
    provider.setEmail("musee_louvre@paris.fr");
    provider.setName("Musée du Louvre");
    provider.setPhoneNumber("+33144556677");
    provider.setImage("img/dummies/slides/04_thumb.png");
    provider.setMap("http://goo.gl/maps/BT94M");
    providerDao.create(provider);
    //Provider 1
    Provider provider1 = new Provider();
    provider1.setAddress(address);
    provider1.setDescription("Eperon de Versailles bla bla bla description");
    provider1.setEmail("eperon@versailles.fr");
    provider1.setName("Eperon de Versailles");
    provider1.setPhoneNumber("+33144556677");
    provider1.setImage("img/dummies/slides/05_thumb.png");
    provider1.setMap("https://maps.google.fr/maps?q=Club+Hippique+de+Versailles,+Rue+R%C3%A9mont,+Versailles&hl=fr&ie=UTF8&sll=48.856614,2.352222&sspn=0.15654,0.41851&oq=club+hipp&hq=Club+Hippique+de+Versailles,&hnear=Rue+R%C3%A9mont,+78000+Versailles&t=m&z=16");
    providerDao.create(provider1);
    //Provider 2
    Provider provider2 = new Provider();
    provider2.setAddress(address);
    provider2.setDescription("Centre de comestique francais bla bla bla description");
    provider2.setEmail("beauty_success@paris.fr");
    provider2.setName("Beauty Success");
    provider2.setPhoneNumber("+33144556677");
    provider2.setImage("img/dummies/slides/06_thumb.png");
    provider2.setMap("https://maps.google.fr/maps?q=Beauty+Success,+Rue+du+G%C3%A9n%C3%A9ral+de+Gaulle,+Rambouillet&hl=fr&ie=UTF8&sll=48.791518,2.156053&sspn=0.009796,0.026157&oq=beauty+success+ram&hq=Beauty+Success,&hnear=Rue+du+G%C3%A9n%C3%A9ral+de+Gaulle,+78120+Rambouillet&t=m&z=16");
    providerDao.create(provider2);

    // User
    User user = new User();
    user.setBirthday(new Date(1977, 04, 04));
    user.setEmail("romaingallan@gmail.com");
    user.setFirstName("Romain");
    user.setLastName("Gallan");
    user.setType(User.Type.ADMINISTRATOR);
    user.setAddress(address);
    userDao.create(user);

    //  Authentication
    Authentication authentication = new Authentication();
    authentication.setLogin("rom");
    authentication.setPassword("gal");
    authentication.setUser(user);
    authenticationDao.create(authentication);

    // Product1
    Product product1 = new Product();
    product1.setProvider(provider);
    product1.setName("Visite du Louvre");
    product1.setPrice(20.50);
    product1.setDescription("Visite de deux heures avec guide");
    product1.setLimitedDate(new Date(2013, 11, 01));
    product1.setCreationDate(new Date());
    product1.setProductType(ProductType.Culture);
    product1.setImage("img/dummies/slides/01_thumb.png");
    productDao.create(product1);
    // Product2
    Product product2 = new Product();
    product2.setProvider(provider1);
    product2.setName("Balade à cheval");
    product2.setPrice(15.50);
    product2.setDescription("Parcourer la plage à cheval.");
    product2.setLimitedDate(new Date(2013, 11, 01));
    product2.setCreationDate(new Date());
    product2.setProductType(ProductType.Sport);
    product2.setImage("img/dummies/slides/02_thumb.png");
    productDao.create(product2);
    // Product3
    Product product3 = new Product();
    product3.setProvider(provider2);
    product3.setName("Soin du visage");
    product3.setPrice(20.50);
    product3.setDescription("Soin vivifiant aux algues vertes de Touraine.");
    product3.setLimitedDate(new Date(2013, 11, 01));
    product3.setCreationDate(new Date());
    product3.setProductType(ProductType.Beaute);
    product3.setImage("img/dummies/slides/03_thumb.png");
    productDao.create(product3);

    //Account
    Account account = new Account();
    account.setCreationDate(new Date());
    account.setUser(user);
    accountDao.create(account);

    //Line Item
    Lineitem lineitem1 = new Lineitem();
    lineitem1.setProduct(product1);
    lineitem1.setQuantity(2);
    lineDao.create(lineitem1);

    Lineitem lineitem2 = new Lineitem();
    lineitem2.setProduct(product2);
    lineitem2.setQuantity(1);
    lineDao.create(lineitem2);

    //Order
    Order order = new Order();
    order.setOrderStatus(OrderStatus.PAID);
    order.setCreationDate(new Date());
    //order.setTotalPrice(21.20);
    order.setAccount(account);
    order.getLineItems().add(lineitem1);
    order.getLineItems().add(lineitem2);
    orderDao.create(order);

    //Payment
    Payment payment = new Payment();
    payment.setOrder(order);
    payment.setDatePayment(new Date());
    payment.setTotalPrice(order.getTotalPrice());
    paymentDao.create(payment);

    //  Retrieves
    List<Address> addresses = addressDao.retrieveAll();
    List<User> users = userDao.retrieveAll();
    List<Authentication> authentications = authenticationDao.retrieveAll();
    List<Product> products = productDao.retrieveAll();
    List<Order> orders = orderDao.retrieveAll();
    List<Lineitem> lines = lineDao.retrieveAll();
    List<Provider> providers = providerDao.retrieveAll();

    return null;
  }

  public String injectCart()
  {
    int quantity = 0;
    for (Product product : productDao.retrieveAll())
    {
      CartMBean.LineitemBean cartLineitem = cartMBean.new LineitemBean();
      cartLineitem.setIdProduct(product.getIdProduct());
      cartLineitem.setQuantity(quantity++);
      cartMBean.getLineitemBeans().add(cartLineitem);
    }
    return null;
  }

  public void setCartMBean(CartMBean aCartMBean)
  {
    cartMBean = aCartMBean;
  }

  @EJB
  protected AuthenticationDao authenticationDao;
  @EJB
  protected UserDao userDao;
  @EJB
  protected AddressDao addressDao;
  @EJB
  protected ProductDao productDao;
  @EJB
  protected AccountDao accountDao;
  @EJB
  protected OrderDao orderDao;
  @EJB
  protected PaymentDao paymentDao;
  @EJB
  protected LineitemDao lineDao;
  @EJB
  protected ProviderDao providerDao;

  @ManagedProperty("#{cartMBean}")
  protected CartMBean cartMBean;
}
